﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECare.BUS;
using ECare.BUS.Data;
using ECare.Client.frms;

namespace ECare.Win.controls
{
    public partial class ucSupply : DevExpress.XtraEditors.XtraUserControl
    {
        public ucSupply()
        {
            InitializeComponent();


            this.windowsUIButtonPanel1.ButtonClick += windowsUIButtonPanel1_ButtonClick;
            this.Load += ucSupply_Load;
        }

        void windowsUIButtonPanel1_ButtonClick(object sender, DevExpress.XtraBars.Docking2010.ButtonEventArgs e)
        {
            if (e.Button.Properties.Caption == "Edit")
            {
                if (this.grd.SelectedRows.Count == 0)
                    return;

                var entitye = this.grd.SelectedRows[0].DataBoundItem as ResourceSupplyInfo;
                var frm = new frmSupplyInfo {entity = entitye};
                frm.Saved += frm_Saved;
                frm.ShowDialog();
            }
            else if (e.Button.Properties.Caption == "New")
            {
                var frm = new frmSupplyInfo {entity = null};
                frm.Saved += frm_Saved;
                frm.ShowDialog();

            }else
            {


            }

        }

        void frm_Saved(object entity)
        {
            if (source == null)
                source = new BindingList<ResourceSupplyInfo>();
            var role = entity as ResourceSupplyInfo;
            var findresult = source.FirstOrDefault(a => a.resource_supply_id == role.resource_supply_id);
            if (findresult == null)
                this.source.Add(role);
            else
            {
            }
        }

        public BindingList<ResourceSupplyInfo> source { get; set; }
        void ucSupply_Load(object sender, EventArgs e)
        {
            source = new BindingList<ResourceSupplyInfo>(ResourceSupplyHelper.GetResources());
            this.grd.DataSource = source;

            //if (!CheckPermission(14))
            //    this.btnAdd.Enabled = false;

            //if (!CheckPermission(15))
            //    this.btnEdit.Enabled = false;

        }
    }
}
